//ajax配置
import axios from 'axios'
import store from '../store/index.js'
import qs from 'qs'
import router from '../router/index.js'
import {toast} from '../util/tool.js'



axios.defaults.timeout = 10000;
// axios.defaults.baseURL = 'http://api.test.biz.jioao.cn'; // 测试
axios.defaults.baseURL = 'http://api.biz.nongtebest.com'; // 农特正式接口地址


axios.interceptors.request.use((config) => {
    var token = store.state.token; 
	if (config.method === 'post') {
	    config.headers['Content-Type'] = 'application/json';
        config.headers['userToken'] = token;
        config.data = JSON.stringify(config.data);
        // console.log(config);
        return config;
	}
 //    if(config.url =='/yg_selectmall/appAccount/login'){
 //        return config;
 //    }else if(token != ''){
 //        config.headers['token'] = token;
 //        return config;
 //    }else{
        // router.replace({
        //     path: '/login'
        // });
    // }		
},(error) => {
	var str = "输入有误，请核对";
	toast(str, 2, false);
	return;
});

axios.interceptors.response.use((res) => {
	if(res.data.code == 20100){
        router.replace({
            path: '/'
        });
    }
    console.log(res);
    return res;
}, (error) => {
	var str = "网络异常";
	toast(str, 2, false);
});


export function fetch(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, params)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err);
            })
    })
}